package main

import (
	"flag"
	"github.com/gin-gonic/gin"
	"os"
)

// main 程序主入口，初始化配置并启动HTTP服务
func main() {
	setupLogger()

	defaultFilePath := flag.String("filePath", "", "The path of the docker-compose.yml file needs to be "+
		"provided when the program is running or the interface is called.")
	flag.StringVar(&authToken, "token", "", "Authentication token.")
	flag.StringVar(&listenPort, "listen", "5001", "Port to listen on (default 5001)")
	flag.StringVar(&logLevel, "loglevel", "debug", "Log level (debug, info, warn, error, fatal, panic)")
	flag.Parse()

	if authToken == "" {
		log.Fatal("Startup failed: Token is empty")
		os.Exit(1)
	}

	// 创建 DockerComposeManager 实例
	manager := NewDockerComposeManager(*defaultFilePath, authToken, log)

	gin.SetMode(gin.ReleaseMode)
	router := gin.Default()

	// 使用管理器方法处理路由
	router.POST("/update-service", func(c *gin.Context) {
		manager.UpdateDockerCompose(c)
	})
	router.POST("/rollback-service", func(c *gin.Context) {
		manager.RollbackService(c)
	})

	// 添加健康检查端点
	router.GET("/health", manager.HealthCheck)

	log.Infof("Server is running on port %s...", listenPort)
	err := router.Run(":" + listenPort)
	if err != nil {
		log.Errorf("Failed to start server: %s", err)
	}
}
